package com.wyait.manage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyait.manage.dao.ArticleMapper;
import com.wyait.manage.pojo.Article;
import com.wyait.manage.pojo.User;
import com.wyait.manage.utils.PageDataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 文章service
 */


@Service
public class ArticleServiceImpl implements ArticleService{
    @Autowired(required = false)
    private ArticleMapper articleMapper;
    @Autowired
    private LogSystemService logSystemService;


    @Override
    public PageDataResult getArticleList(Article article, Integer page, Integer limit) {
        PageDataResult pdr = new PageDataResult();
        PageHelper.startPage(page, limit);
        List<Article> articles = articleMapper.getArticleList(article);
        // 获取分页查询后的数据
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        // 设置获取到的总记录数total：
        pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
        pdr.setList(articles);
        return pdr;

    }

    @Override
    public String setArticle(User currentUser, Article article) {
        article.setCreateTime(new Date());
        int i = 0;


        //日志名称(哪个类)
        String logName = "文章";
        //日志记录的数据
        Map<String, String> logDataMap = new HashMap<>();
        logDataMap.put("logName",logName);
        logDataMap.put("type","1");   //type=1,代表为文章


        //如果id不为空执行更新操作
        if(article.getId() != null){
            //日志记录 文章修改信息(要在更新文章表数据前获取,不然获取到的是更新后的数据)
            Article oldArticle = articleMapper.getArticleById(article.getId());
            logDataMap.put("oldName",oldArticle.getTitle());
            logDataMap.put("newName",article.getTitle());

            //更新文章表数据
            i = articleMapper.updateArticle(article);

        }
        //id为空执行新增操作
        else{
            i = articleMapper.insert(article);

            //日志记录 文章新增信息
            logDataMap.put("newName",article.getTitle());
        }



        if (i != 0){
            //日志记录
            logSystemService.setLog(currentUser,logDataMap);
            return "ok";
        }else{
            return "[新增或更新]轮播图失败";
        }
    }


    /**
     *根据id删除文章
     */
    @Override
    public String delArticle(User currentUser,Integer id) {
        int i = 0;


        //日记记录的数据(在删除文张之前获取,不然获取不到信息)
        Article delArticle = articleMapper.getArticleById(id);
        String delName = delArticle.getTitle();
        //日志名称(哪个类)
        String logName = "文章";

        //删除文章
        i = articleMapper.delArticle(id);
        if (i != 0){
            //日志记录 目录删除信息
            Integer type = 2; //类型为2,代表为目录

            logSystemService.setDelLog(currentUser,logName,delName,type);

            return "ok";
        }else{
            return "删除失败";
        }
    }


    /**
     *根据id查询文章
     */
    @Override
    public Article getArticleById(Integer id) {
        Article article = articleMapper.getArticleById(id);
        return article;
    }


}
